package surreal.triviamaster.gui;

import surreal.triviamaster.R;
import surreal.triviamaster.beans.Question;
import surreal.triviamaster.database.LocalCache;
import surreal.triviamaster.utilities.TriviaMasterApplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Incorrect extends Activity{

	private LocalCache db;
	private Question question;
	private String yourAnswer="";
	private boolean isQuizOver=false;

	@Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.incorrect);
	    
	  //get db, increase quiz index, get question
	    db = ((TriviaMasterApplication) getApplication()).getLocalCache();
	    question = db.getCurrentQuizQuestion();
	    
	  //handles changing orientation
    	final Bundle data = (Bundle) getLastNonConfigurationInstance();
    	if(data!=null) {
    		yourAnswer=data.getString("yourAnswer");
    		isQuizOver=data.getBoolean("isQuizOver");
    	} else {
    		yourAnswer=getIntent().getExtras().getString("yourAnswer");
    		isQuizOver = getIntent().getExtras().getBoolean("isQuizOver");
    	}
	    
    	Log.d("Incorrect", "Your Answer: " +yourAnswer);
    	
    	//Displaying the GUI
	    ((TextView) findViewById(R.id.answerGiven)).setText("Your answer: "+getIntent().getExtras().getString("yourAnswer"));
	    ((TextView) findViewById(R.id.acceptableAnswers)).setText("Answer: " + question.getaAs()[0].getAnswer());
	    
	    if(isQuizOver) {
	   	 	((TextView) findViewById(R.id.nextQuestion)).setText("See Results");
	    }
	    
	    //Flag Answer
	    Button flagAnswer = (Button) findViewById(R.id.flagAnswer);
	    flagAnswer.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(Incorrect.this, FlagAnswer.class);
            	intent.putExtra("yourAnswer", yourAnswer);
            	intent.putExtra("isQuizOver", isQuizOver);
	  	      	startActivity(intent);
            }
        });
	    
	  //Cancel
	    Button cancel = (Button) findViewById(R.id.nextQuestion);
	    cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent intent = new Intent(Incorrect.this, Game.class);
	  	      	startActivity(intent);
            }
        });
	}
	
	@Override
	public void onBackPressed() {
	   return;
	}
	
	@Override
	public Bundle onRetainNonConfigurationInstance() {
		Bundle bundle = new Bundle();
		bundle.putString("yourAnswer", yourAnswer);
		bundle.putBoolean("isQuizOver", isQuizOver);
	    return bundle;
	}
}
